package com.uenpay.im.ui.old.activity.group;

import android.content.Intent;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.net.HttpRequestType;
import com.qiyunxin.android.http.utils.Utils;
import com.qyx.android.database.GroupMemberManager;
import com.qyx.android.database.GroupTalkDbManager;
import com.qyx.android.database.TalkMsgManager;
import com.qyx.android.database.TopListMsgManager;
import com.qyx.android.entity.GroupMemberEntity;
import com.qyx.android.entity.GroupTalkEntity;
import com.qyx.android.message.type.PacketType;
import com.uenpay.im.BroadcastAction;
import com.uenpay.im.IMApplication;
import com.uenpay.im.callback.IGetGroupMembersListener;
import com.uenpay.im.callback.IRemoveGroupMemberListener;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.CreateGroupInvokeItem;
import com.uenpay.im.invokeitems.GetGroupMessageDetail;
import com.uenpay.im.invokeitems.GroupAddPersonInvokeItem;
import com.uenpay.im.invokeitems.HttpRequestHandle;
import com.uenpay.im.invokeitems.chat.AcceptJoinGroupInvokeItem;
import com.uenpay.im.invokeitems.chat.ApplyJoinGroupInvokeItem;
import com.uenpay.im.invokeitems.chat.AutoJoinGroupInvokeItem;
import com.uenpay.im.invokeitems.chat.CheckExitInGroupInvokeItem;
import com.uenpay.im.invokeitems.chat.DeleteGroupInvokeItem;
import com.uenpay.im.invokeitems.chat.GetGroupMembersListInvokeItem;
import com.uenpay.im.invokeitems.chat.QuitGroupTalkInvokeItem;
import com.uenpay.im.invokeitems.chat.RemoveGroupMemberInvokeItem;
import com.uenpay.im.invokeitems.chat.SearchGroupsInvokeItem;
import com.uenpay.im.invokeitems.chat.SetChatOpenInvokeItem;
import com.uenpay.im.invokeitems.chat.SetGroupNewManagerInvokeItem;
import com.uenpay.im.invokeitems.chat.UpdateGroupInfoInvokeItem;
import com.uenpay.im.invokeitems.contacts.MyGroupsInvokItem;

import java.util.ArrayList;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 */
public class GroupMangerHandler {

	private static final String TAG = "GroupMangerHandler";

	private GroupTalkDbManager groupTalkDbManager = null;
	private GroupMemberManager GroupMemberManager = null;
	private TalkMsgManager talkMsgManager = null;
	private TopListMsgManager topListMsgManager = null;
	private GroupMemberManager mGroupMemberManager = null;

	public GroupMangerHandler() {
		groupTalkDbManager = new GroupTalkDbManager();
		GroupMemberManager = new GroupMemberManager();
		topListMsgManager = new TopListMsgManager();
		talkMsgManager = new TalkMsgManager();
		mGroupMemberManager = new GroupMemberManager();
	}

	public interface IGroupManagerResultListener {
		void onGroupManagerResult(int status, String msg, String chatId);
	}

	/**
	 * 创建群聊
	 *
	 * @param groupName
	 * @param receive_cust_id
	 * @param amount
	 *            ""：普通群 !""：经费群
	 * @param mIGroupManagerResultListener
	 */
	public void createGroup(String groupName, String receive_cust_id, final String amount,
			final IGroupManagerResultListener mIGroupManagerResultListener) {
		CreateGroupInvokeItem item = new CreateGroupInvokeItem(groupName, receive_cust_id, amount);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {
			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject == null) {
					mIGroupManagerResultListener.onGroupManagerResult(-1, "", "");
				} else {
					CreateGroupInvokeItem.CreateGroupResult result = ((CreateGroupInvokeItem) resultObject).getOutput();
					if (result != null) {
						if (result.status == HttpInvokeResultCode.RESULT_OK) {
							if (!TextUtils.isEmpty(amount)) {
								getGroupDetail(result.chatId, null);
							}
							insertDB(result);
						}
						mIGroupManagerResultListener.onGroupManagerResult(result.status, result.msg, result.chatId);
					} else {
						mIGroupManagerResultListener.onGroupManagerResult(-1, "", "");
					}
				}
			}
		});
	}

	/**
	 * 设置是否可以邀请成员
	 *
	 * @param chatId
	 * @param isOpen
	 * @param mIAcceptJoinGroupListener
	 */
	public static void setInvitMember(String chatId, int isOpen, final IAcceptJoinGroupListener mIAcceptJoinGroupListener) {
		SetChatOpenInvokeItem item = new SetChatOpenInvokeItem(chatId, isOpen);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					SetChatOpenInvokeItem.SetChatOpenInvokeItemResult result = ((SetChatOpenInvokeItem) resultObject).getOutput();
					if (result != null) {
						mIAcceptJoinGroupListener.onAcceptJoinGroupResult(result.status, result.msg);
					}
				} else {
					mIAcceptJoinGroupListener.onAcceptJoinGroupResult(-1, "");
				}
			}
		});
	}

	public interface IGroupResultListener {
		void onGroupResult(int status, String msg);
	}

	/**
	 * 添加群成员
	 *
	 * @param group_type
	 *            群类型1：普通群2：经费群
	 * @param group_id
	 * @param receive_cust_id
	 * @param mIAddGroupMemberResultListener
	 */
	public void addGroupMembers(int group_type, String group_id, String receive_cust_id,
			final IGroupResultListener mIAddGroupMemberResultListener) {
		GroupAddPersonInvokeItem item = new GroupAddPersonInvokeItem(group_type, group_id, receive_cust_id);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					GroupAddPersonInvokeItem.GroupAddPersonInvokeItemResult result = ((GroupAddPersonInvokeItem) resultObject).getOutPut();
					if (result != null) {
						mIAddGroupMemberResultListener.onGroupResult(result.status, result.msg);
					} else {
						mIAddGroupMemberResultListener.onGroupResult(-1, "");
					}
				} else {
					mIAddGroupMemberResultListener.onGroupResult(-1, "");
				}
			}
		});

	}

	public interface IGetGroupDetailListener {
		void onGetGroupDetailResult(GetGroupMessageDetail.GetGroupMessageDetailResult result);
	}

	public void getGroupDetail(final String chatId, final IGetGroupDetailListener mGetGroupDetailListener) {
		GetGroupMessageDetail item = new GetGroupMessageDetail(chatId);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_FAIL_AND_CACHE, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					final GetGroupMessageDetail.GetGroupMessageDetailResult result = ((GetGroupMessageDetail) resultObject).getOutput();
					final GroupTalkDbManager groupTalkDbManager = new GroupTalkDbManager();
					GroupTalkEntity groupTalk = groupTalkDbManager.queryGroupByGroupId(Long.valueOf(chatId));

					if (result != null) {
						if (result.status == HttpInvokeResultCode.RESULT_OK && result.groupTalk != null) {
							GroupMemberManager mGroupMemberManager = new GroupMemberManager();
							groupTalk = result.groupTalk;
							groupTalk.is_admin = 0;
							if (IMApplication.getCustId().equals(result.groupTalk.creator_cust_id + "")) {
								groupTalk.is_admin = 1;
							}
							groupTalkDbManager.insertOrUpdateGroup(groupTalk);
							if (result.participants != null) {
								new UpdateNameInTalkMsgTask(chatId, result.participants).execute();
								for (GroupMemberEntity p : result.participants) {
									p.group_id = result.groupTalk.group_id;
									if (p.cust_id == Long.valueOf(result.groupTalk.creator_cust_id)) {
										p.role = "1";
									} else {
										p.role = "0";
									}
									mGroupMemberManager.insertOrUpdateGroupMember(p);
								}
								for (long cust_id : result.deleteCustIdArr) {
									mGroupMemberManager.deleteGroupMember(result.groupTalk.group_id + "", cust_id + "");
								}
								Intent intent = new Intent(BroadcastAction.GET_TALKDETAIL_ACTION);
								intent.putExtra("status", "success");
								intent.putExtra("chat_id", Integer.valueOf(result.groupTalk.group_id + ""));
								intent.putExtra("name", result.groupTalk.group_name);
								IMApplication.getAppContext().sendBroadcast(intent);
							}
						}
						if (mGetGroupDetailListener != null) {
							mGetGroupDetailListener.onGetGroupDetailResult(result);
						}
					}
				} else {
					mGetGroupDetailListener.onGetGroupDetailResult(null);
				}
			}
		});
	}

	public interface IAcceptJoinGroupListener {
		void onAcceptJoinGroupResult(int status, String msg);
	}

	/**
	 * 同意加入群聊
	 *
	 * @param type
	 *            1:普通群2：经费群
	 * @param notice_id
	 * @param accept
	 * @param invitecustid
	 *            邀请人ID
	 * @param mIAcceptJoinGroupListener
	 */
	public static void acceptJoinGroup(int type, String notice_id, int accept, String paypassword, String chatId, String invitecustid,
			final IAcceptJoinGroupListener mIAcceptJoinGroupListener) {
		AcceptJoinGroupInvokeItem acceptItem = new AcceptJoinGroupInvokeItem(type, notice_id, accept, paypassword, chatId, invitecustid);

		HttpRequestHandle.onBaseRequest(acceptItem, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {

					AcceptJoinGroupInvokeItem.AcceptJoinGroupInvokeItemResult result = ((AcceptJoinGroupInvokeItem) resultObject)
							.getOutput();
					if (result != null) {
						mIAcceptJoinGroupListener.onAcceptJoinGroupResult(result.status, result.msg);
					} else {
						mIAcceptJoinGroupListener.onAcceptJoinGroupResult(-1, "");
					}

				} else {
					mIAcceptJoinGroupListener.onAcceptJoinGroupResult(-1, failReason);
				}
			}
		});

	}

	/**
	 * 申请加入群聊
	 *
	 * @param chatId
	 * @param mRemoveGroupMemberListener
	 */
	public void applyJoinGroup(String chatId, final IRemoveGroupMemberListener mRemoveGroupMemberListener) {

		ApplyJoinGroupInvokeItem item = new ApplyJoinGroupInvokeItem(chatId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					ApplyJoinGroupInvokeItem.ApplyJoinGroupInvokeItemResult result = ((ApplyJoinGroupInvokeItem) resultObject).getOutput();
					int status = -1;
					if (result != null) {
						IMApplication.showToast(result.msg);
						status = result.status;
					}
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(status);
				} else {
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(-1);
				}
			}
		});
	}

	public interface IAutoJoinListener {
		void onAutoJoinResult(int status, String msg);
	}

	/**
	 * @param chatId
	 * @param mIAutoJoinListener
	 */
	public void checkExitInGroup(String chatId, final IAutoJoinListener mIAutoJoinListener) {
		CheckExitInGroupInvokeItem item = new CheckExitInGroupInvokeItem(chatId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					CheckExitInGroupInvokeItem.CheckExitInGroupInvokeItemResult result = ((CheckExitInGroupInvokeItem) resultObject)
							.getOutput();
					int status = -1;
					if (result != null) {
						status = result.status;
					}
					mIAutoJoinListener.onAutoJoinResult(status, result.msg);
				} else {
					mIAutoJoinListener.onAutoJoinResult(-1, failReason);
				}
			}
		});
	}

	/**
	 * @param chatId
	 * @param mIAutoJoinListener
	 */
	public void autoJoinGroup(String chatId, final IAutoJoinListener mIAutoJoinListener) {
		AutoJoinGroupInvokeItem item = new AutoJoinGroupInvokeItem(chatId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					AutoJoinGroupInvokeItem.AutoJoinGroupInvokeItemResult result = ((AutoJoinGroupInvokeItem) resultObject).getOutput();
					int status = -1;
					if (result != null) {
						IMApplication.showToast(result.msg);
						status = result.status;
					}
					mIAutoJoinListener.onAutoJoinResult(status, result.msg);
				} else {
					mIAutoJoinListener.onAutoJoinResult(-1, failReason);
				}
			}
		});

	}

	/**
	 * 修改群资料
	 *
	 * @param chat_id
	 *            群id
	 * @param name
	 *            修改的名称
	 * @param type
	 *            1：群名称2我在群聊的名称3群简介
	 */
	public void updateGroupInfo(final String chat_id, final String name, final int type, final IGroupResultListener mGroupResultListener) {
		UpdateGroupInfoInvokeItem item = new UpdateGroupInfoInvokeItem(chat_id, name, type);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					UpdateGroupInfoInvokeItem.UpdateGroupInfoInvokeItemResult result = ((UpdateGroupInfoInvokeItem) resultObject)
							.getOutput();
					if (result != null) {
						if (mGroupResultListener != null) {
							mGroupResultListener.onGroupResult(result.status, result.msg);
						}

						if (result.status == HttpInvokeResultCode.RESULT_OK) {
							if (type == 1) {
								groupTalkDbManager.updateGroupName(Long.valueOf(chat_id), name);
								/** 通知会话首页刷新 **/
								Log.d(TAG, "sendBroadcast TAKEING_MSG_TOP_MSG");
								IMApplication.getAppContext().sendBroadcast(new Intent(BroadcastAction.TAKEING_MSG_TOP_MSG));
							} else if (type == 2) {
								mGroupMemberManager.updateName(chat_id, IMApplication.getCustId(), name);
							}

						}
					} else {
						if (mGroupResultListener != null) {
							mGroupResultListener.onGroupResult(-1, failReason);
						}
					}
				} else {
					if (mGroupResultListener != null) {
						mGroupResultListener.onGroupResult(-1, failReason);
					}
				}
			}
		});

	}

	/**
	 *
	 * @param chatId
	 * @param custId
	 * @param mRemoveGroupMemberListener
	 */
	public void setGroupNewManager(String chatId, String custId, final IRemoveGroupMemberListener mRemoveGroupMemberListener) {

		SetGroupNewManagerInvokeItem item = new SetGroupNewManagerInvokeItem(chatId, custId);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					SetGroupNewManagerInvokeItem.SetGroupNewManagerInvokeItemResult result = ((SetGroupNewManagerInvokeItem) resultObject)
							.getOutput();
					int status = -1;
					if (result != null) {
						IMApplication.showToast(result.msg);
						status = result.status;
					}
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(status);
				} else {
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(-1);
				}
			}
		});

	}

	/**
	 *
	 * @param chatId
	 * @param user_id
	 * @param mRemoveGroupMemberListener
	 */
	public void removeGroupMember(final String chatId, final String user_id, final IRemoveGroupMemberListener mRemoveGroupMemberListener) {
		RemoveGroupMemberInvokeItem item = new RemoveGroupMemberInvokeItem(chatId, user_id);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					RemoveGroupMemberInvokeItem.RemoveGroupMemberInvokeItemResult result = ((RemoveGroupMemberInvokeItem) resultObject)
							.getOutput();
					int status = -1;
					if (result != null) {
						status = result.status;
						if (status == HttpInvokeResultCode.RESULT_OK) {
							if (user_id.indexOf(",") > 0) {
								String[] ids = user_id.split(",");
								for (int i = 0, size = ids.length; i < size; i++) {
									GroupMemberManager.deleteGroupMember(chatId, ids[i]);
								}
							} else {
								GroupMemberManager.deleteGroupMember(chatId, user_id);
							}
						}
						IMApplication.showToast(result.msg);
					}
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(status);
				} else {
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(-1);
				}
			}
		});

	}

	/**
	 * 退出群聊
	 *
	 * @param chat_id
	 * @param mRemoveGroupMemberListener
	 */
	public void exitGroup(final String chat_id, final int optype, final IRemoveGroupMemberListener mRemoveGroupMemberListener) {
		QuitGroupTalkInvokeItem item = new QuitGroupTalkInvokeItem(chat_id, optype);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					HttpInvokeResult result = ((QuitGroupTalkInvokeItem) resultObject).getOutput();
					if (result != null) {
						mRemoveGroupMemberListener.onRemoveGroupMemberResult(result.status);
						if (result.status == HttpInvokeResultCode.RESULT_OK) {
							deleteLocalGroupInfo(chat_id);
							Intent intent = new Intent();
							intent.setAction(BroadcastAction.CLEAR_TALK_MSG_ACTION);
							IMApplication.getAppContext().sendBroadcast(intent);
						}
						IMApplication.showToast(result.msg);
					}
				} else {
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(-1);
				}
			}
		});

	}

	/**
	 * 删除群聊
	 *
	 * @param chat_id
	 * @param mRemoveGroupMemberListener
	 */
	public void deleteGroup(final String chat_id, final IRemoveGroupMemberListener mRemoveGroupMemberListener) {
		DeleteGroupInvokeItem item = new DeleteGroupInvokeItem(chat_id);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_ONLY, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					DeleteGroupInvokeItem.DeleteGroupResult result = ((DeleteGroupInvokeItem) resultObject).getOutput();
					if (result != null) {
						mRemoveGroupMemberListener.onRemoveGroupMemberResult(result.status);
						IMApplication.showToast(result.msg);
					}
					if (result != null && result.status == HttpInvokeResultCode.RESULT_OK) {
						deleteLocalGroupInfo(chat_id);
						Intent intent = new Intent();
						intent.setAction(BroadcastAction.CLEAR_TALK_MSG_ACTION);
						IMApplication.getAppContext().sendBroadcast(intent);
					}
				} else {
					mRemoveGroupMemberListener.onRemoveGroupMemberResult(-1);
				}
			}
		});

	}

	/**
	 * 删除本地某个群资料
	 *
	 * @param ChatId
	 */
	private void deleteLocalGroupInfo(String ChatId) {
		/** 删除本地该群资料 **/
		groupTalkDbManager.deleteGroup(Long.valueOf(ChatId));
		/** 删除该群的所有群成员 **/
		GroupMemberManager.deleteGroupAllMembers(Long.valueOf(ChatId));
		/** 删除该群的聊天记录 **/
		delDBmsg(ChatId);
	}

	/**
	 * 删除群聊对应的聊天记录
	 *
	 * @param chatId
	 */
	private void delDBmsg(String chatId) {
		talkMsgManager.deleteGroupAllMsgByGroupId(Long.valueOf(chatId));
		topListMsgManager.deleteByMsg(Long.valueOf(chatId), Integer.valueOf(PacketType.GROUP));
		Intent update_top = new Intent(BroadcastAction.CLEAR_TALK_MSG_ACTION);
		IMApplication.getAppContext().sendBroadcast(update_top);
		Intent intent = new Intent();
		intent.setAction(BroadcastAction.CLEAR_MSG_ACTION);
		IMApplication.getAppContext().sendBroadcast(intent);
	}

	/**
	 * 获取群成员
	 */
	public void getGroupMembers(final String group_id, final String creator_cust_id, final IGetGroupMembersListener getGroupMembersListener) {
		GetGroupMembersListInvokeItem item = new GetGroupMembersListInvokeItem(group_id);
		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_FAIL_AND_CACHE, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					GetGroupMembersListInvokeItem.GetGroupMembersListInvokeItemResult result = ((GetGroupMembersListInvokeItem) resultObject)
							.getOutput();
					if (result != null && result.status == HttpInvokeResultCode.RESULT_OK) {
						updateGroupMembers(group_id, creator_cust_id, result.participants);
						getGroupMembersListener.getGroupMembers(result.participants);
					}
				} else {
					getGroupMembersListener.getGroupMembers(null);
				}
			}
		});

	}

	/**
	 * @param keyWord
	 * @param page
	 * @param mSearchGroupsListener
	 */
	public void searchGroupsByKeyWord(String keyWord, int page, final IGetMyGroupsListener mSearchGroupsListener) {
		SearchGroupsInvokeItem item = new SearchGroupsInvokeItem(keyWord, page);

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_FAIL_AND_CACHE, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					SearchGroupsInvokeItem.SearchGroupsInvokeItemResult result = ((SearchGroupsInvokeItem) resultObject).getOutput();
					if (result != null) {
						if (mSearchGroupsListener != null) {
							mSearchGroupsListener.onGetMyGroupsResult(result.status, result.msg, result.groups);
						}
					}
				} else {
					mSearchGroupsListener.onGetMyGroupsResult(-1, failReason, null);
				}
			}
		});

	}

	public interface IGetMyGroupsListener {
		void onGetMyGroupsResult(int status, String msg, ArrayList<GroupTalkEntity> groups);
	};

	/**
	 * 获取我的群
	 *
	 * @param mGroupsListener
	 */
	public void getMyGroups(final IGetMyGroupsListener mGroupsListener) {
		MyGroupsInvokItem item = new MyGroupsInvokItem();

		HttpRequestHandle.onBaseRequest(item, HttpRequestType.SERVER_FAIL_AND_CACHE, new HttpRequestHandle.IRequestResultListener() {

			@Override
			public void onResult(Object resultObject, boolean isCache, String failReason) {
				if (resultObject != null) {
					MyGroupsInvokItem.MyGroupsInvokItemResult result = ((MyGroupsInvokItem) resultObject).getOutput();
					if (result != null) {
						if (result.groups != null && result.groups.size() > 0) {
							new UpdateGroupInfoTask(result.groups).execute();
						}
						if (mGroupsListener != null) {
							mGroupsListener.onGetMyGroupsResult(result.status, result.msg, result.groups);
						}
					} else {
						if (mGroupsListener != null) {
							mGroupsListener.onGetMyGroupsResult(-1, "", null);
						}
					}
				} else {
					if (mGroupsListener != null) {
						mGroupsListener.onGetMyGroupsResult(-1, "", null);
					}
				}
			}
		});

	}

	/**
	 * 将创建的群聊添加到数据库中
	 *
	 * @param result
	 */
	private void insertDB(CreateGroupInvokeItem.CreateGroupResult result) {
		GroupTalkDbManager groupTalkDbManager = new GroupTalkDbManager();
		GroupTalkEntity groupTalk = new GroupTalkEntity();
		groupTalk.group_id = Long.valueOf(result.chatId);
		groupTalk.group_name = result.name;
		groupTalk.created_date = Utils.getCurrentTimeMills();
		groupTalk.is_top = 0;
		groupTalk.is_admin = 1;
		groupTalk.max_count = result.cust_max_count;
		groupTalk.join_status = 2;
		groupTalk.has_count = result.cust_count;
		groupTalkDbManager.insertOrUpdateGroup(groupTalk);

	}

	private void updateGroupMembers(final String group_id, final String creator_cust_id, final ArrayList<GroupMemberEntity> _ArrayList) {
		if (TextUtils.isEmpty(creator_cust_id) || _ArrayList == null || _ArrayList.size() <= 0) {
			return;
		}
		new Thread(new Runnable() {

			@Override
			public void run() {
				for (GroupMemberEntity p : _ArrayList) {
					p.group_id = Long.valueOf(group_id);
					if (p.cust_id == Long.valueOf(creator_cust_id)) {
						p.role = "1";
					} else {
						p.role = "0";
					}
					GroupMemberManager.insertOrUpdateGroupMember(p);
				}
			}
		}).start();
	}

	private class UpdateNameInTalkMsgTask extends AsyncTask<Void, Void, Void> {
		private String chat_id;
		private ArrayList<GroupMemberEntity> arrayList;

		/**
		 * @param _chat_id
		 * @param _arrayList
		 */
		public UpdateNameInTalkMsgTask(String _chat_id, ArrayList<GroupMemberEntity> _arrayList) {
			chat_id = _chat_id;
			arrayList = _arrayList;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			for (int i = 0, size = arrayList.size(); i < size; i++) {
				talkMsgManager.updateMsgFromCustNameByCustId(arrayList.get(i).nick_name, arrayList.get(i).cust_id + "", chat_id);
			}
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
		}

	}

	/**
	 *
	 * @author SL
	 *
	 */
	private class UpdateGroupInfoTask extends AsyncTask<Void, Void, Void> {

		private ArrayList<GroupTalkEntity> arrayList = null;

		/**
		 *
		 * @param _ArrayList
		 */
		public UpdateGroupInfoTask(ArrayList<GroupTalkEntity> _ArrayList) {
			arrayList = _ArrayList;
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			for (int i = 0, size = arrayList.size(); i < size; i++) {
				groupTalkDbManager.insertOrUpdateGroup(arrayList.get(i));
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
		}

	}

}
